package com.leetroll.app.controller;

import com.leetroll.api.service.CategoryService;
import com.leetroll.api.service.ItemService;
import com.leetroll.api.service.MetricService;
import com.leetroll.app.utils.AppJwtTokenUtil;
import com.leetroll.common.bean.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;


@RestController
@RefreshScope
@RequestMapping("/redis")
@Api(value = "缓存接口", tags = {"缓存接口"})
@Slf4j
public class RedisController {

    private final StringRedisTemplate redisTemplate;

    @Autowired
    public RedisController(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @ApiOperation(value = "删除所有键值对", notes = "删除所有键值对")
    @GetMapping("/flush-all")
    public ApiResponse<?> flushAll() {
        Set<String> keys = redisTemplate.keys("*");
        if (keys != null) {
            redisTemplate.delete(keys);
        }
        return ApiResponse.OK();
    }

    @ApiOperation(value = "所有键值对", notes = "所有键值对")
    @GetMapping("/keys")
    public ApiResponse<Set<String>> AllKeys() {
        Set<String> keys = redisTemplate.keys("*");
        return ApiResponse.OK(keys);
    }

}
